package org.real.guetshop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.real.guetshop.constant.MessageConstant;
import org.real.guetshop.constant.PasswordConstant;
import org.real.guetshop.exception.AccountNotExitException;
import org.real.guetshop.exception.PasswordErrorException;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.real.guetshop.mapper.EmployeeMapper;
import org.real.guetshop.pojo.dto.EmployeeDTO;
import org.real.guetshop.pojo.dto.EmployeePageQueryDTO;
import org.real.guetshop.result.PageResult;
import org.real.guetshop.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.real.guetshop.pojo.dto.EmployeeLoginDTO;
import org.real.guetshop.pojo.entity.Employee;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param employeeLoginDTO
     * @return
     */
    @SneakyThrows
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //根据用户名查询数据库
        Employee employee = employeeMapper.getByUsername(username);

        //处理各种异常情况（用户名不存在，密码错误等）
        if (employee == null) {
            //账号不存在
            throw new AccountNotExitException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码对比
        // TODO 后期要进行密码加密
        if(!password.equals(employeeLoginDTO.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //返回实体对象
        return employee;
    }

    /**
     * 新增员工
     * @param employeeDTO
     */
    @Override
    public void addNewEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置账号状态
//        employee.setStatus(1);

        //设置默认密码123456
        employee.setPassword(PasswordConstant.DEFAULT_PASSWORD);

        //插入数据
        employeeMapper.insertNewEmployee(employee);
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // select * from employee limit 0,10
        //开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());

        Page<Employee> pageResult = employeeMapper.pageQuery(employeePageQueryDTO);

        long total = pageResult.getTotal();
        List<Employee> records = pageResult.getResult();

        return new PageResult(total,records);
    }

    /**
     * 删除员工
     * @param employeeDTO
     */
    @Override
    public void deleteEmployee(EmployeeDTO employeeDTO) {
        employeeMapper.deleteEmployee(employeeDTO);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     */
    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        //对象属性拷贝
        Employee employee = employeeMapper.getByUsername(employeeDTO.getUsername());
        BeanUtils.copyProperties(employeeDTO, employee);

        employeeMapper.updateEmployee(employee);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public Employee getById(Integer id) {
        return employeeMapper.getById(id);
    }
}
